package com.hanthink.web.controller.app.manhourfact;

import com.hanthink.common.constant.HttpStatus;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.core.text.Convert;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.manhour.FactWorkerUsed;
import com.hanthink.system.service.manhour.IFactWorkerUsedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/manhourfact/actuallabor")
public class ActuallaborControll extends BaseController {

    @Autowired
    private IFactWorkerUsedService factWorkerUsedService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查詢实际用工明细
     */
    @PostMapping("/query")
    public TableDataInfo queryactuallabor(@RequestBody Map<String, Object> map) throws ParseException {
        FactWorkerUsed factWorkerUsed= new FactWorkerUsed();
        factWorkerUsed.setCompanyid(map.get("companyid").toString());
        Date date = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        date = formatter.parse(map.get("planworkdatetime").toString().replace("/","-"));
        factWorkerUsed.setPlanworkdatetime(date);

        if (map.get("pshiftid").toString().equals("1")){
            factWorkerUsed.setPshiftid("白班");
        }
        else if(map.get("pshiftid").toString().equals("2")){
            factWorkerUsed.setPshiftid("夜班");
        }
        else if(map.get("pshiftid").toString().equals("3")){
            factWorkerUsed.setPshiftid("中班");
        }
        else if(map.get("pshiftid").toString().equals("4")){
            factWorkerUsed.setPshiftid("转班(白)");
        }
        else if(map.get("pshiftid").toString().equals("5")){
            factWorkerUsed.setPshiftid("转班(夜)");
        }
        factWorkerUsed.setWorkertype(map.get("workertype").toString());

        List<FactWorkerUsed> list = factWorkerUsedService.selectFactWorkerUsedList(factWorkerUsed);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }
    /**
     * 更新实际用工明细
     */
    @PostMapping("/update")
    public TableDataInfo updateactuallabor(@RequestBody Map<String, Object> map) throws ParseException {
        FactWorkerUsed factWorkerUsed= new FactWorkerUsed();
        factWorkerUsed.setId(Convert.toLong(map.get("id").toString()));
        factWorkerUsed.setFactworksused(Convert.toInt(map.get("factworksused").toString()));
        factWorkerUsed.setWorkhour(Convert.toFloat(map.get("workhour").toString()));

        int i = factWorkerUsedService.updateFactWorkerUsed(factWorkerUsed);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("更新成功");
        return tableDataInfo;
    }
    /**
     * 删除实际用工明细
     */
    @PostMapping("/delete")
    public TableDataInfo deleteactuallabor(@RequestBody Map<String, Object> map) throws ParseException {
        int i = factWorkerUsedService.deleteFactWorkerUsedById(Convert.toLong(map.get("id").toString()));
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("删除成功");
        return tableDataInfo;
    }
    /**
     * 保存实际用工明细
     */
//    @GetMapping("/test")
//    public String testactuallabor() throws ParseException {
//
//        return "tableDataInfo";
//    }
//
//
//    @PostMapping("/import")
//    public TableDataInfo importactuallabor(@RequestBody Map<String, Object> map) throws ParseException {
//        TableDataInfo tableDataInfo = new TableDataInfo();
//        tableDataInfo.setCode(HttpStatus.SUCCESS);
//        tableDataInfo.setMsg("保存成功");
//        return tableDataInfo;
//    }
    @PostMapping("/save")
    public TableDataInfo saveactuallabor(@RequestBody Map<String, Object> map) throws ParseException {
        FactWorkerUsed factWorkerUsed= new FactWorkerUsed();
        //部门代码
        factWorkerUsed.setCompanyid(map.get("companyid").toString());
        //日期
        Date date = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        date = formatter.parse(map.get("planworkdatetime").toString().replace("/","-"));
        factWorkerUsed.setPlanworkdatetime(date);
        //班次
        if (map.get("pshiftid").toString().equals("1")){
            factWorkerUsed.setPshiftid("白班");
        }
        else if(map.get("pshiftid").toString().equals("2")){
            factWorkerUsed.setPshiftid("夜班");
        }
        else if(map.get("pshiftid").toString().equals("3")){
            factWorkerUsed.setPshiftid("中班");
        }
        else if(map.get("pshiftid").toString().equals("4")){
            factWorkerUsed.setPshiftid("转班(白)");
        }
        else if(map.get("pshiftid").toString().equals("5")){
            factWorkerUsed.setPshiftid("转班(夜)");
        }
        //用工类型
        factWorkerUsed.setWorkertype("CZ");
        //实际用工人数
        factWorkerUsed.setFactworksused(Convert.toInt(map.get("factworksused")));
        //实际用工工时
        factWorkerUsed.setWorkhour(Convert.toFloat(map.get("workhour")));
        //创建人
        factWorkerUsed.setCreator(map.get("creator").toString());
        //创建时间
        Date now = new Date();
        factWorkerUsed.setCreatedatetime(now);
        //审核人
        factWorkerUsed.setChecktor(map.get("checktor").toString());
        //审核状态
        factWorkerUsed.setChecked(Convert.toInt(map.get("checked").toString()));
        //审核时间
        factWorkerUsed.setCheckdatetime(now);

        factWorkerUsedService.insertFactWorkerUsed(factWorkerUsed);


        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("保存成功");
        return tableDataInfo;
    }
}
